package com.sy.service.impl;


import com.sy.bean.client.Client;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.exception.BusinessException;
import com.sy.mapper.client.ClientContactMapper;
import com.sy.mapper.client.ClientMapper;
import com.sy.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl  extends BaseServiceImpl<Client, ClientMapper> implements ClientService {

    @Autowired
    ClientMapper clientMapper;
    @Autowired
    ClientContactMapper clientContactMapper;
    @Override
    public int save(Client entity) {
        //保存客户
        int rs=super.save(entity);
        if (rs==0){
            throw new BusinessException("保存客户失败");
        }
        //保存联系人
        rs=clientContactMapper.batchSave(entity.getId(), entity.getClientContactList());
        if (rs==0)
            throw new BusinessException("保存联系人失败");
        return 1;
    }

    @Override
    public int update(Client entity) {
        //更新客户
        int rs=baseMapper.update(entity);
        if (rs==0)
            throw new BusinessException("更新客户失败");
        //更新联系人
        //先删除之前的联系人
        clientContactMapper.deleteByCLienId(entity.getId());
        //生成联系人
        rs=clientContactMapper.batchSave(entity.getId(),entity.getClientContactList());
        if (rs==0)
            throw new BusinessException("更新联系人失败");
        return 1;
    }

    @Override
    public int batchDelete(List ids) {
        return clientMapper.batchDelete(ids);
    }
}

